<?php
declare (strict_types=1);

namespace addons\BuilderPlus\contract\event;

class ConditionParser
{
    public static function parse($data, $condition, $logic): bool
    {
        $pass = false;
        foreach ($condition as $item) {
            $res = self::compare($data, $item);
            if ($logic == 'or') {
                if ($res) return true;
            } else {
                if (!$res) {
                    return false;
                } else {
                    $pass = true;
                }
            }

        }
        return $pass;
    }

    public static function compare($data, $item): bool
    {
        [$name, $symbol, $val] = $item;
        switch ($symbol) {
            case "===":
                return $data[$name] === $val;
            case "==":
                return $data[$name] == $val;
            case ">":
                return $data[$name] > $val;
            case ">=":
                return $data[$name] >= $val;
            case "<":
                return $data[$name] < $val;
            case "<=":
                return $data[$name] <= $val;
            case "<>":
                return $data[$name] != $val;
            default:
                return false;
        }
    }
}